package com.example.wacoffee_manage.controller;

import com.example.wacoffee_manage.Service.userService;
import com.example.wacoffee_manage.common.PageResult;
import com.example.wacoffee_manage.pojo.User;
import com.example.wacoffee_manage.utils.OssUtils;
import com.example.wacoffee_manage.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
@Slf4j
public class userController {
    @Autowired
    private userService userService;
    @GetMapping
    public Result getAllUsers(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false, name = "userName") String username,
            @RequestParam(required = false, name = "gender") Integer gender,
            @RequestParam(required = false, name = "phone") String phone,
            @RequestParam(required = false, name = "image") String image,
            @RequestParam(required = false, name = "id")Integer id){
        User user = new User();
        user.setPage(currentPage);
        user.setPageSize(pageSize);
        if (username != null) user.setUserName(username);
        if (gender != null) user.setGender(gender);
        if (phone != null) user.setPhone(phone);
        if (image != null) user.setImage(image);
        if (id != null) user.setId(id);
        PageResult pageResult = userService.getAllUsers(user);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable int id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam int id){
        userService.delete(id);
        return Result.success();
    }

    @PutMapping("/recover")
    public Result recover(@RequestParam int id){
        userService.recover(id);
        return Result.success();
    }

    @PostMapping("/importUser")
    public Result importUserData(MultipartFile file) throws IOException {
        userService.importUserData(file);
        return Result.success();
    }
}
